package com.example.zx.theline.lyjh.lyyjh;

import android.content.Context;
import android.os.IBinder;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.zx.theline.R;

import Utils.BackHandlerHelper;
import app.MyApplication;
import base.BaseActivity;
import com.example.zx.theline.lyjh.lyyjh.JhshcxFragment;
import com.example.zx.theline.lyjh.lyyjh.JhshyjhFragment;
import com.example.zx.theline.lyjh.lyyjh.JhshywcFragment;
import com.example.zx.theline.lyjh.lyyjh.JhspywcFragment;

/**
 * Changed by Jianqi on 2017\11\2.
 * <p>
 * 邻营月计划 主界面
 */
public class JhshActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {
    private RadioGroup mRadioGroup;
    private Fragment mJhshyjhFragment, mJhshywcFragment, mJhspywcFragment, mJhshcxFragment;
    private RadioButton shyjh, shywc, spywc, yjhcx;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jhsh);
        InitView();
        InitData();
        InitListen();
    }

    @Override
    protected void InitView() {
        mRadioGroup = (RadioGroup) findViewById(R.id.radiogroup);
        shyjh = (RadioButton) findViewById(R.id.shyjh);
        shywc = (RadioButton) findViewById(R.id.shywc);
        spywc = (RadioButton) findViewById(R.id.spywc);
        yjhcx = (RadioButton) findViewById(R.id.yjhcx);
    }

    @Override
    protected void InitData() {
        if (MyApplication.getmLoginUser().getSgyjh().isShyjh() && MyApplication.getmLoginUser().getSgyjh().isShywc() && MyApplication.getmLoginUser().getSgyjh().isSpywc() && MyApplication.getmLoginUser().getSgyjh().isYjhcx()) {
            displayView(shyjh, 0, 0, 0, 0, 0);
        } else if (!MyApplication.getmLoginUser().getSgyjh().isShyjh() && MyApplication.getmLoginUser().getSgyjh().isShywc() && MyApplication.getmLoginUser().getSgyjh().isSpywc() && MyApplication.getmLoginUser().getSgyjh().isYjhcx()) {
            displayView(shywc, 8, 0, 0, 0, 1);
        } else if (MyApplication.getmLoginUser().getSgyjh().isShyjh() && !MyApplication.getmLoginUser().getSgyjh().isShywc() && MyApplication.getmLoginUser().getSgyjh().isSpywc() && MyApplication.getmLoginUser().getSgyjh().isYjhcx()) {
            displayView(shyjh, 0, 8, 0, 0, 0);
        } else if (MyApplication.getmLoginUser().getSgyjh().isShyjh() && MyApplication.getmLoginUser().getSgyjh().isShywc() && !MyApplication.getmLoginUser().getSgyjh().isSpywc() && MyApplication.getmLoginUser().getSgyjh().isYjhcx()) {
            displayView(shyjh, 0, 0, 8, 0, 0);
        } else if (MyApplication.getmLoginUser().getSgyjh().isShyjh() && MyApplication.getmLoginUser().getSgyjh().isShywc() && MyApplication.getmLoginUser().getSgyjh().isSpywc() && !MyApplication.getmLoginUser().getSgyjh().isYjhcx()) {
            displayView(shyjh, 0, 0, 0, 8, 0);
        } else if (!MyApplication.getmLoginUser().getSgyjh().isShyjh() && !MyApplication.getmLoginUser().getSgyjh().isShywc() && MyApplication.getmLoginUser().getSgyjh().isSpywc() && MyApplication.getmLoginUser().getSgyjh().isYjhcx()) {
            displayView(spywc, 8, 8, 0, 0, 2);
        } else if (!MyApplication.getmLoginUser().getSgyjh().isShyjh() && MyApplication.getmLoginUser().getSgyjh().isShywc() && !MyApplication.getmLoginUser().getSgyjh().isSpywc() && MyApplication.getmLoginUser().getSgyjh().isYjhcx()) {
            displayView(shywc, 8, 0, 8, 0, 1);
        } else if (!MyApplication.getmLoginUser().getSgyjh().isShyjh() && MyApplication.getmLoginUser().getSgyjh().isShywc() && MyApplication.getmLoginUser().getSgyjh().isSpywc() && !MyApplication.getmLoginUser().getSgyjh().isYjhcx()) {
            displayView(shywc, 8, 0, 0, 8, 1);
        } else if (MyApplication.getmLoginUser().getSgyjh().isShyjh() && !MyApplication.getmLoginUser().getSgyjh().isShywc() && !MyApplication.getmLoginUser().getSgyjh().isSpywc() && MyApplication.getmLoginUser().getSgyjh().isYjhcx()) {
            displayView(shyjh, 0, 8, 8, 0, 0);
        } else if (MyApplication.getmLoginUser().getSgyjh().isShyjh() && !MyApplication.getmLoginUser().getSgyjh().isShywc() && MyApplication.getmLoginUser().getSgyjh().isSpywc() && !MyApplication.getmLoginUser().getSgyjh().isYjhcx()) {
            displayView(shyjh, 0, 8, 0, 8, 0);
        } else if (MyApplication.getmLoginUser().getSgyjh().isShyjh() && MyApplication.getmLoginUser().getSgyjh().isShywc() && !MyApplication.getmLoginUser().getSgyjh().isSpywc() && !MyApplication.getmLoginUser().getSgyjh().isYjhcx()) {
            displayView(shyjh, 0, 0, 8, 8, 0);
        } else if (!MyApplication.getmLoginUser().getSgyjh().isShyjh() && !MyApplication.getmLoginUser().getSgyjh().isShywc() && !MyApplication.getmLoginUser().getSgyjh().isSpywc() && MyApplication.getmLoginUser().getSgyjh().isYjhcx()) {
            displayView(yjhcx, 8, 8, 8, 0, 3);
        } else if (!MyApplication.getmLoginUser().getSgyjh().isShyjh() && !MyApplication.getmLoginUser().getSgyjh().isShywc() && MyApplication.getmLoginUser().getSgyjh().isSpywc() && !MyApplication.getmLoginUser().getSgyjh().isYjhcx()) {
            displayView(spywc, 8, 8, 0, 8, 2);
        } else if (!MyApplication.getmLoginUser().getSgyjh().isShyjh() && MyApplication.getmLoginUser().getSgyjh().isShywc() && !MyApplication.getmLoginUser().getSgyjh().isSpywc() && !MyApplication.getmLoginUser().getSgyjh().isYjhcx()) {
            displayView(shywc, 8, 0, 8, 8, 1);
        } else if (MyApplication.getmLoginUser().getSgyjh().isShyjh() && !MyApplication.getmLoginUser().getSgyjh().isShywc() && !MyApplication.getmLoginUser().getSgyjh().isSpywc() && !MyApplication.getmLoginUser().getSgyjh().isYjhcx()) {
            displayView(shyjh, 0, 8, 8, 8, 0);
        }
    }

    @Override
    protected void InitListen() {
        mRadioGroup.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        switch (checkedId) {
            case R.id.shyjh:
                setSelect(0);
                break;
            case R.id.shywc:
                setSelect(1);
                break;
            case R.id.spywc:
                setSelect(2);
                break;
            case R.id.yjhcx:
                setSelect(3);
                break;
        }
    }

    //控制界面中按钮的显示及默认选中状态
    private void displayView(RadioButton mRadioBtn, int a, int b, int c, int d, int e) {
        shyjh.setVisibility(a);
        shywc.setVisibility(b);
        spywc.setVisibility(c);
        yjhcx.setVisibility(d);
        mRadioBtn.setChecked(true);
        setSelect(e);
    }

    //Fragment的选择事件
    private void setSelect(int i) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        hideFragment(transaction);
        switch (i) {
            case 0:
                //审核月计划
                if (MyApplication.getmLoginUser().getSgyjh().isShyjh()) {
                    shyjh.setVisibility(View.VISIBLE);
                    if (mJhshyjhFragment == null) {
                        mJhshyjhFragment = new JhshyjhFragment();
                        transaction.add(R.id.framelayout, mJhshyjhFragment);
                    }
                } else {
                    shyjh.setVisibility(View.GONE);
                }
                transaction.show(mJhshyjhFragment);
                break;
            case 1:
                //审核已完成
                if (MyApplication.getmLoginUser().getSgyjh().isShywc()) {
                    shywc.setVisibility(View.VISIBLE);
                    if (mJhshywcFragment == null) {
                        mJhshywcFragment = new JhshywcFragment();
                        transaction.add(R.id.framelayout, mJhshywcFragment);
                    }
                } else {
                    shywc.setVisibility(View.GONE);
                }
                transaction.show(mJhshywcFragment);
                break;
            case 2:
                //审批已完成
                if (MyApplication.getmLoginUser().getSgyjh().isSpywc()) {
                    spywc.setVisibility(View.VISIBLE);
                    if (mJhspywcFragment == null) {
                        mJhspywcFragment = new JhspywcFragment();
                        transaction.add(R.id.framelayout, mJhspywcFragment);
                    }
                } else {
                    spywc.setVisibility(View.GONE);
                }
                transaction.show(mJhspywcFragment);
                break;
            case 3:
                //月计划查询
                if (MyApplication.getmLoginUser().getSgyjh().isYjhcx()) {
                    yjhcx.setVisibility(View.VISIBLE);
                    if (mJhshcxFragment == null) {
                        mJhshcxFragment = new JhshcxFragment();
                        transaction.add(R.id.framelayout, mJhshcxFragment);
                    }
                } else {
                    yjhcx.setVisibility(View.GONE);
                }
                transaction.show(mJhshcxFragment);
                break;
        }
        transaction.commit();
    }

    //隐藏Fragment
    private void hideFragment(FragmentTransaction transaction) {
        if (mJhshyjhFragment != null) {
            transaction.hide(mJhshyjhFragment);
        }
        if (mJhshywcFragment != null) {
            transaction.hide(mJhshywcFragment);
        }
        if (mJhspywcFragment != null) {
            transaction.hide(mJhspywcFragment);
        }
        if (mJhshcxFragment != null) {
            transaction.hide(mJhshcxFragment);
        }
    }

    //处理系统的返回键
    @Override
    public void onBackPressed() {
        if (!BackHandlerHelper.handleBackPress(this)) {
            super.onBackPressed();
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View view = getCurrentFocus();
            if (isHideInput(view, ev)) {
                HideSoftInput(view.getWindowToken());
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    // 判定是否需要隐藏
    private boolean isHideInput(View v, MotionEvent ev) {
        if (v != null && (v instanceof EditText)) {
            int[] l = {0, 0};
            v.getLocationInWindow(l);
            int left = l[0], top = l[1], bottom = top + v.getHeight(), right = left
                    + v.getWidth();
            if (ev.getX() > left && ev.getX() < right && ev.getY() > top
                    && ev.getY() < bottom) {
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

    // 隐藏软键盘
    private void HideSoftInput(IBinder token) {
        if (token != null) {
            InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            manager.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

}
